﻿<%@ WebHandler Language="C#" Class="FundStat" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class FundStat : AjaxHandlerBase
{
    public FundStat()
    {
        //导出
        htTypePermit.Add("export", Constants.Permit_FuntStat_Export);
    }
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //添加报备相关信息
                case "getlist":
                    GetList(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    //查询
    private void GetList(HttpContext context)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
        sb.Append(" b.Status=1 AND b.bArchivement = 1 ");
        if (!string.IsNullOrEmpty(context.Request["sDate"]))
        {
            string[] sdate = context.Request["sDate"].Split('-');
            sb.AppendFormat(" and (b.ArchivementYear>{0} ", sdate[0]);
            sb.AppendFormat(" or (b.ArchivementYear={0} and b.ArchivementMonth>={1})) ", sdate[0], sdate[1].TrimStart('0'));
        }
        if (!string.IsNullOrEmpty(context.Request["eDate"]))
        {
            string[] edate = context.Request["eDate"].Split('-');
            sb.AppendFormat(" and (b.ArchivementYear<{0} ", edate[0]);
            sb.AppendFormat(" or (b.ArchivementYear={0} and b.ArchivementMonth<={1})) ", edate[0], edate[1].TrimStart('0'));
        }

        sb1.Append(" k.IsFinish = 1 AND k.status = 1 AND k.FundName in ('上家佣金','下家佣金','上家退佣','下家退佣') ");
        if (!string.IsNullOrEmpty(context.Request["SGetDate"]))
        {
            string[] sgdate = context.Request["SGetDate"].Split('-');
            sb1.AppendFormat(" and (k.AchievementYear>{0} ", sgdate[0]);
            sb1.AppendFormat(" or (k.AchievementYear={0} and k.AchievementMonth>={1})) ", sgdate[0], sgdate[1].TrimStart('0'));
        }
        if (!string.IsNullOrEmpty(context.Request["EGetDate"]))
        {
            string[] egdate = context.Request["EGetDate"].Split('-');
            sb1.AppendFormat(" and (k.AchievementYear<{0} ", egdate[0]);
            sb1.AppendFormat(" or (k.AchievementYear={0} and k.AchievementMonth<={1})) ", egdate[0], egdate[1].TrimStart('0'));
        }
        //4级
        Guid city = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
        Guid company = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
        Guid businessdistrict = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaid"]);
        Guid departs = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);

        if (departs != Guid.Empty)
        {
            sb.AppendFormat(" and a.departid = '{0}'", departs);
        }
        else if (businessdistrict != Guid.Empty)
        {
            Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
            string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, businessdistrict);
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
            item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
            if (!cs.Hit)
            {
                item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(businessdistrict);
                if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
            }

            if (item == null)
            {
                context.Response.Write(retExecResult(Constants.ExecResult.Error, "获取商圈或分类数据丢失", ""));
            }
            sb.AppendFormat(" and (a.departid in (Select DepartId from v_Hr_DepartmentInfo where DepartSrc like '{0}%'))", item.DepartSrc);
        }
        else if (company != Guid.Empty)
        {
            sb.AppendFormat(" and (a.departid in (select DepartId from v_Hr_DepartmentInfo WHERE companyid='{0}')) ", company);
        }
        else if (city != Guid.Empty)
        {
            sb.AppendFormat(" and (a.departid in (Select DepartId from v_Hr_DepartmentInfo WHERE cityid='{0}')) ", city);
        }
        else
        {
            sb.AppendFormat(" and (a.departid in (select DepartId from v_Hr_DepartmentInfo WHERE companyid='{0}')) ", CurrentUserInfo.CompanyId);
        }

        string strSql = "select dbo.FN_GetBussinessByDepartID(a.DepartId) as business,dbo.FN_GetDepartNameByDepartID(a.DepartId) as depart,"
                        + "sum((b.VRealCommision+b.BRealCommision)*a.Point/10) as oldcommision,"
                        + "sum((VRealCommisionFinal+BRealCommisionFinal-b.VSTDiscount - b.BSTDiscount)*a.Point/10) as curcommision,"
                        + "sum((isnull(BillMoney,0))*a.Point/10) as getcommision," +
                          "case sum((b.VRealCommision+b.BRealCommision)*a.Point/10) when 0 then 0 else sum((isnull(BillMoney,0))*a.Point/10)/sum((b.VRealCommision+b.BRealCommision)*a.Point/10) end as oldrate,"
                        + "case sum((VRealCommisionFinal+BRealCommisionFinal-b.VSTDiscount - b.BSTDiscount)*a.Point/10) when 0 then 0 else sum((isnull(BillMoney,0))*a.Point/10)/sum((VRealCommisionFinal+BRealCommisionFinal-b.VSTDiscount - b.BSTDiscount)*a.Point/10) end as currate"
                        + " from "
                        + "t_Contract_BaseInfo as b inner join t_Contract_CommisionInfo as c on b.ContractId=c.ContractId "
                        + "inner join dbo.t_Contract_PointAllotInfo as a on c.ContractId=a.ContractId left join "
                        + "(select contractId, sum(BillMoney*InOrOut) as BillMoney from t_Contract_Finance_BillInfo k where " + sb1.ToString() +" group by ContractID) d "
                        + "On a.contractId = d.contractId "
                        //+ "t_Contract_Finance_BillInfo as d on a.ContractId=d.ContractId and d.Status=1 and  "
                        + "where " + sb.ToString()
                        + " group by a.DepartId";

        DataSet ds = Chenxin.Lib.Business.DAL.v_Contract_CommisionDetailInfo.GetList(strSql);
        context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1}}}}}", ds.Tables[0].Rows.Count, Chenxin.Lib.Utility.JsonHelp.Convert2Json(ds.Tables[0])));
    }
}